package ebusiness.ebusiness.Controllers;

import ebusiness.ebusiness.Dao.DBManager;
import ebusiness.ebusiness.Dao.GoodsDao.GoodQuery;
import ebusiness.ebusiness.Dao.GoodsDao.GoodQueryVO;
import ebusiness.ebusiness.Entity.Good;
import ebusiness.ebusiness.Entity.UserEntity.RegisteUser;
import ebusiness.ebusiness.Entity.UserEntity.User;
import ebusiness.ebusiness.Entity.UserEntity.Visitor;
import ebusiness.ebusiness.Mapper.PathUtil.PagePath;
import ebusiness.ebusiness.Mapper.PathUtil.WebPath;


import ebusiness.ebusiness.Models.Front.ShoppingTorlley.Toy;
import org.apache.tomcat.util.security.MD5Encoder;

import ebusiness.ebusiness.Models.Front.ShoppingTorlley.CartView;
import ebusiness.ebusiness.Models.Front.ShoppingTorlley.TorlleyImp;
import org.apache.ibatis.session.SqlSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

@Controller
public class VisitController {
    @RequestMapping("/")
    public String HomePage(Model md, HttpSession session) throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
    	try {

        List<Map<String,Object>> goods=sqlSession.getMapper(GoodQuery.class).findAllgoods();
            System.out.println(goods);

        sqlSession.close();
    	md.addAttribute("goods",goods);
    	}
    	catch(Exception e) {

    		e.printStackTrace();
    		sqlSession.close();
    	}

    	
        md.addAttribute("name","Home pAGE");
        if(session.getAttribute("user")==null){
          //  Visitor visitor=new Visitor();
            session.setAttribute("user",-1);
        }

      return PagePath.HOMEPAGE;
    }
    @RequestMapping(path = WebPath.GOODDEATAIL+"{good_id}")
    public String Gooddeatil(@PathVariable("good_id")int id,Model model,HttpSession session) throws Exception{
        //model.addAttribute("id",id);
        GoodQueryVO goodQueryVO=new GoodQueryVO();
        Good gd = goodQueryVO.getGoodByID(id);
        model.addAttribute("good",gd);
       
        return PagePath.GOOD;
    }

    @RequestMapping(path = WebPath.CART,method = RequestMethod.GET)
    public String Cart(Model model,HttpSession session)throws Exception{
        TorlleyImp tp=null;
        Object obj=session.getAttribute("cart");
        if(obj==null){
            //error handel
            Object us =session.getAttribute("user");
            int user=-1;
            if (us!=null)user=(int)us;
            else session.setAttribute("user",us);
            if(user <=0){
                tp= TorlleyImp.NewInstance();
                tp.SetUser(user);
            }
            else {

                tp= TorlleyImp.HistoryCart( user);
                tp.SetUser(user);
            }
        }else {
            tp=(TorlleyImp) session.getAttribute("cart");
        }
        session.setAttribute("cart",tp);
        final  int x=tp.getGoods().size();

        CartView cv = new CartView() {
            @Override
            public void showCart(List<Toy> goods) {
                if(x>0){
                    model.addAttribute("goods",goods);
                   double t=0;
                   for(Toy tt:goods)
                       t=t+tt.getNumber()*tt.getGood_price();
                    model.addAttribute("total",t);

                }else {
                    model.addAttribute("empty","there are no goods in cart");
                }

            }
        };
        tp.show(cv);
        
        return PagePath.SHOWCART;
    }
}
